#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "data.h"

struct sData
{
	char * str;
	int tam;
};

Data
data_fromString(char *s)
{
	Data d;
	d = (Data) malloc (sizeof(struct sData));
	if (d!=NULL)
	{
		d->tam = strlen(s);		
		d->str = (char *) calloc ((d->tam)+1,sizeof(char));
		if (d->str == NULL)
		{
			free(d);
			d = NULL;			
			perror("No hay memoria para string de nuevo data(data_fromString)");
			exit(EXIT_FAILURE);
		}
		d->str = strncpy(d->str,s,(d->tam)+1);
	}
	else
		exit(EXIT_FAILURE);

return d;
}

char *
data_toString(Data d)
{
	char * s;
	s = (char *) calloc ((d->tam)+1,sizeof(char));
	if (s!=NULL)
		s = strncpy(s,d->str,(d->tam)+1);
	else
		exit(EXIT_FAILURE);

return s;
}

int
data_length(Data d)
{
return (d->tam);
}

Data
data_clone(Data d)
{
return (data_fromString(d->str));
}

Data
data_destroy(Data d)
{
	if (d!=NULL)
	{
		free(d->str);
		d->str = NULL;
		free(d);
		d = NULL;
	}
return d;
}
